package com.yanfan.energy.service.impl;

import com.yanfan.energy.domain.EnergyPriceHistory;
import com.yanfan.energy.mapper.EnergyPriceHistoryDao;
import com.yanfan.energy.service.EnergyPriceHistoryService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 单价设置历史记录表(EnergyPriceHistory)表服务实现类
 *
 * @author makejava
 * @since 2022-09-28 11:19:20
 */
@Service("energyPriceHistoryService")
@Transactional(rollbackFor = Exception.class)
public class EnergyPriceHistoryServiceImpl implements EnergyPriceHistoryService {
    @Resource
    private EnergyPriceHistoryDao energyPriceHistoryDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public EnergyPriceHistory queryById(Integer id) {
        return this.energyPriceHistoryDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param energyPriceHistory 筛选条件
     * @param pageRequest        分页对象
     * @return 查询结果
     */
    @Override
    public Page<EnergyPriceHistory> queryByPage(EnergyPriceHistory energyPriceHistory, PageRequest pageRequest) {
        long total = this.energyPriceHistoryDao.count(energyPriceHistory);
        return new PageImpl<>(this.energyPriceHistoryDao.queryAllByLimit(energyPriceHistory, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param energyPriceHistory 实例对象
     * @return 实例对象
     */
    @Override
    public EnergyPriceHistory insert(EnergyPriceHistory energyPriceHistory) {
        this.energyPriceHistoryDao.insert(energyPriceHistory);
        return energyPriceHistory;
    }

    /**
     * 修改数据
     *
     * @param energyPriceHistory 实例对象
     * @return 实例对象
     */
    @Override
    public EnergyPriceHistory update(EnergyPriceHistory energyPriceHistory) {
        this.energyPriceHistoryDao.update(energyPriceHistory);
        return this.queryById(energyPriceHistory.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.energyPriceHistoryDao.deleteById(id) > 0;
    }

    /**
     * 查询能源单价设置历史记录
     *
     * @return java.util.List<io.github.jhipster.sample.sansheng.entity.EnergyPriceHistory>
     * @author caijinkang
     * @date 2022/9/28 11:23
     */
    @Override
    public List<EnergyPriceHistory> selectAllEnergyPriceHistory(String types) {
        List<EnergyPriceHistory> histories = this.energyPriceHistoryDao.selectAllEnergyPriceHistory(types);
        histories.forEach(h -> {
            EnergyPriceHistory.Type.Item hi = EnergyPriceHistory.Type.getByCode(h.getType());
            if (Objects.nonNull(hi)) {
                h.setName(hi.getName());
            }
        });
        return histories;
    }
}
